/**
 * 
sudo rm -rf /deploy/src/pay-admin

*/
const shell = require('shelljs');// 引入shell.js等依赖

const dir = shell.pwd();//项目 pay-deploy的目录
const deploy = "/deploy";//部署的目录
if (shell.test('-e', deploy + "/mysql")) {//如果旧的部署目录存在
    //sudo cp -rf /deploy/mysql/  /backup/mysql
    shell.cp(deploy + "/mysql", "/backup/mysql");//把旧的数据库备份一下
    //停掉docker-compse （注：在.sh中不能停掉docker，原因未知。）
    // sh ./shell/docker_stop.sh 

}
shell.cp('-rf', dir + "/deploy_target_template/*", deploy);//复制出部署样板
shell.cp('-rf', dir + "/shell/deploy.sh", deploy + "/deploy.sh");//复制shell命令到部署目录
shell.mkdir('-p', deploy + "/SQL/");
shell.cp('-rf', dir + "/SQL/databaseInit.sql", deploy + "/SQL/");//复制数据库初始化sql。
shell.cp('-rf', dir + "/docker_compose", deploy + "/docker_compose");//复制docker_compose

const src_path = "/deploy/src";//代码存放根目录

//授权，在child_process中无法授权，需要到terminal中执行授权命令
//shell.chmod('-vR', 'a+rwx', src_path);// chmod -vR a+rwx /deploy/src  

if (!shell.test('-e', src_path))//'-e', 'src_path': true if path exists    如果存放目录不存在，则创建
    shell.mkdir('-p', src_path)


async function build() {

    const buildEnv = 'local';//要构建的环境
    const config_target_dir = "/deploy/config";//pay-admin、pay-server、pay-batch配置的存放目录

    const pay_admin = require('./deploy/pay-admin.js');//支付管理后台

    const admin_git = "https://git.***.cn/charge/pay-admin.git";//支付管理后台后端
    const admin_webapp_dir = "/deploy/pay_admin/webapp";//支付管理后台 jar部署目标
    const admin_src_dir = await pay_admin.pay_admin(src_path, admin_git, admin_webapp_dir);//拉取代码部署支付管理后台
    const admin_yml = "application-adminEnv.yml";//"application-" + env + ".yml";
    pay_admin.copy_config(admin_src_dir, admin_yml, config_target_dir);



    const pay_admin_web = 'pay-web';//支付管理后台前端页面
    const admin_web_git = 'https://git.***.cn/charge/pay-web.git';//
    const static_dir = '/deploy/pay_admin/static';
    await pay_admin.pay_admin_web(src_path, pay_admin_web, admin_web_git, static_dir, buildEnv);


    const pay = require('./deploy/pay-server_batch.js');//支付接口、批处理
    const pay_git = 'https://git.***.cn/charge/pay.git';
    const server_webapp_dir = '/deploy/pay_server/webapp', batch_webapp_dir = '/deploy/pay_batch/webapp';
    const pay_src_dir = await pay.pay(src_path, pay_git, server_webapp_dir, batch_webapp_dir);
    const server_yml = "application-serverEnv.yml";//复制pay-server的配置
    pay.copy_config(pay_src_dir + "/server", server_yml, config_target_dir);
    const batch_yml = "application-batchEnv.yml";//复制pay-batch的配置
    pay.copy_config(pay_src_dir + "/batch", batch_yml, config_target_dir);


    const pay_gateway = require('./deploy/pay-gateway.js');//支付 通道网关
    const gateway_git = 'https://git.***.cn/charge/pay-gateway.git';
    const gateway_deploy_dir = '/deploy/pay_gateway';
    await pay_gateway.pay_gateway(src_path, gateway_git, gateway_deploy_dir);



    const pay_api_demo = require('./deploy/pay-api-demo.js');//支付 演示示例
    const api_demo_git = 'https://git.***.cn/charge/pay-api-demo.git';
    const pai_demo_deploy_dir = '/deploy/pay_api_demo';
    await pay_api_demo.pay_api_demo(src_path, api_demo_git, pai_demo_deploy_dir);


    //删除源代码
    shell.rm("-rf", src_path);

}

function zip() {
    const cmd = 'zip -r deploy.zip ./*'
    let environment = { ...process.env };
    const options = { async: false, silent: false, cwd: '/deploy', env: environment, shell: '/bin/bash' };

    //阻塞的方式
    const result = shell.exec(cmd, options);
    //输出执行结果
    console.log(result.stdout);
    console.log(result.stderr);
    return result.code;//0是执行成功，其它值是执行失败
}

async function start() {
    //生成运行文件
    await build();

    //打包成zip
    zip();
}

start();

//解压缩命令是： unzip -o -d /deploy deploy.zip
